<?php
/**
 * This file is part of League of Legends PHP Inibin Parser.
 *
 * League of Legends PHP Inibin Parser is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * League of Legends PHP Inibin Parser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with League of Legends PHP Inibin Parser.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * League of Legends Client-Database parser
 * Class: sql
 * 
 * @author Marcel Klimek <klimek@secondred.de>
 * @copyright Marcel Klimek
 * @version $Id: class.sql.php 3 2011-11-12 10:07:44Z naffto88@gmail.com $
 */
class mysql 
{

    /**
     * Stores the sql connection
     * 
     * @var int
     */
    private $resID = 0;

    
    /**
     * Constructor
     * 
     * @access public
     */
    function __construct() {}

    
    /*
     * connects to the database
     *
     * @param string $strHost server address
     * @param string $strUser username
     * @param string $strPassword password
     * @param string $strDatabase database
     *
     * @access public
     */

    public function connectToDB($strHost, $strUser, $strPassword, $strDatabase) 
    {
        @$this->resID = mysql_connect($strHost, $strUser, $strPassword);
        
        if ($this->resID) 
        {
            $this->selectDB($strDatabase);
        } else 
        {
            $this->showError("No Connection possible: " . mysql_error());
        }
    }

    
    /*
     *selects the sql database
     *
     * @param string $strDatabase sql table
     *
     * @access private
     */
    private function selectDB($strDatabase) 
    {
        if (@!mysql_select_db($strDatabase, $this->resID)) 
        {
            $this->showError("Can't use database: " . $strDatabase . " Error: " . mysql_error($this->resID));
        }
    }

    
    /*
     * just throws an error and interrupts the script
     *
     * @param string $strError errormessage
     *
     * @access private
     */

    private function showError($strError) 
    {
        die($strError);
    }

    
    /*
     * Executes an sql query
     *
     * @param string $strQuery sql-query string
     *
     * @access public
     * @return MySQL-recource ID
     */
    public function safe_query($strQuery = "") 
    {
        if (empty($strQuery))
        {
            return false;
        }

        $result = mysql_query($strQuery) or die('Query failed: ' .
                        '<li>errorno=' . mysql_errno() .
                        '<li>error=' . mysql_error() .
                        '<li>query=' . $strQuery);

        return $result;
    }

}

?>
